﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using QuanLyThuPhiTaiLieu.Model;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace QuanLyThuPhiTaiLieu.Biz
{
    public class DanhMucBoSungBiz : BaseBiz
    {
        public int SaveItem(DanhMucBoSung item)
        {
            var dbContext = new Context();

            dbContext.DanhMucBoSungs.Add(item);
            int newId = dbContext.SaveChanges();

            return newId;
        }

        public void UpdateItem(DanhMucBoSung item)
        {
            var dbContext = new Context();
            dbContext.Entry(item).State = EntityState.Modified;
            dbContext.SaveChanges();
        }

        public List<DanhMucBoSung> ViewAll()
        {
            var dbContext = new Context();
            return (from item in dbContext.DanhMucBoSungs
                    orderby item.Sbq ascending
                    select item).ToList();
        }

        public DanhMucBoSung LoadItem(int id)
        {
            var dbContext = new Context();
            return (from item in dbContext.DanhMucBoSungs
                    where item.Id == id
                    select item).First();
        }

        public void RemoveItem(List<DanhMucBoSung> items)
        {
            var dbContext = new Context();
            foreach (var tailieu in items)
            {
                dbContext.DanhMucBoSungs.Attach(tailieu);
                dbContext.DanhMucBoSungs.Remove(tailieu);
                dbContext.Entry(tailieu).State = EntityState.Deleted;
            }

            dbContext.SaveChanges();
        }

        public List<DanhMucBoSungInfo> GetAll(int danhMucId)
        {
            var param = new SqlParameter { ParameterName = "@DanhMucId", Value = danhMucId, DbType = System.Data.DbType.Int32 };

            var dbContext = new Context();
            var items = dbContext.Database.SqlQuery<DanhMucBoSungInfo>("ViewDanhMucBoSung @DanhMucId", param).ToList();

            return items;
        }
        
        public DanhMucBoSungInfo GetItemInfo(int id)
        {
            return null;
            //var dbContext = new Context();
            //return (from item in dbContext.DanhMucBoSungs
            //        join parent in dbContext.KhaiThacVaSuDungTaiLieus on item.KhaiThacVaSuDungTaiLieu equals parent.Id
            //        where item.Id == id
            //        orderby item.Sbq ascending
            //        select new DanhMucBoSungInfo
            //        {
            //            Id = item.Id,
            //            Sqb = item.Sbq,
            //            DangLoaiTaiLieu = parent.DangLoaiTaiLieu,
            //            DonViTinh = parent.DonViTinh,
            //            Khlt = item.Khlt,
            //            LoaiTaiLieu = parent.LoaiTaiLieu,
            //            MaTaiLieu = parent.MaTaiLieu,
            //            MucThuIn = parent.MucThuIn,
            //            MucThuOffice = parent.MucThuOffice,
            //            MucThuPhoto = parent.MucThuPhoto,
            //            MucThuRastor = parent.MucThuRastor,
            //            MucThuVector = parent.MucThuVector,
            //            NoiDung = item.NoiDung,
            //            TenLoaiTaiLieu = parent.TenLoaiTaiLieu
            //        }).First();
        }

        public List<DanhMucBoSung> GetAllBanVe(int danhMucId, int thamKhaoId, int khaiThacId)
        {
            var dbContext = new Context();
            if (thamKhaoId > 0)
            {
                return (from item in dbContext.DanhMucBoSungs
                        where item.ThamKhao == thamKhaoId && item.DanhMuc == danhMucId
                        orderby item.NoiDung ascending
                        select item).ToList();
            }
            else
            {
                return (from item in dbContext.DanhMucBoSungs
                        where item.KhaiThacVaSuDungTaiLieu == khaiThacId && item.DanhMuc == danhMucId
                        orderby item.NoiDung ascending
                        select item).ToList();
            }
        }
    }
}
